# -*- coding:utf-8 -*-
# @FileName  :t16.py
# @Time      :2021/7/15 16:03
import io
import re
import sys
import execjs
import requests
import urllib3

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='gb18030')
session = requests.session()


def jie(v14, v142, pa):
    with open('t14.js', 'r', encoding='gbk') as f:
        jstext = f.read()
    ctx = execjs.compile(jstext)
    result = ctx.call('ccc', v14, v142, pa)
    return result


def main(pa):
    global num
    headers1 = {
        "Accept": "*/*",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Connection": "keep-alive",
        "Host": "match.yuanrenxue.com",
        "Referer": "https://match.yuanrenxue.com/match/14",
        "User-Agent": "yuanrenxue.project",
        "X-Requested-With": "XMLHttpRequest",
        "Cookie": f"sessionid=xexwqahsdzxv9ak5ec4q2xozc32r2yel;mz=TW96aWxsYSxOZXRzY2FwZSw1LjAgKFdpbmRvd3MgTlQgMTAuMDsgV2luNjQ7IHg2NCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgQ2hyb21lLzkyLjAuNDUxNS4xMzEgU2FmYXJpLzUzNy4zNixbb2JqZWN0IE5ldHdvcmtJbmZvcm1hdGlvbl0sdHJ1ZSwsW29iamVjdCBHZW9sb2NhdGlvbl0sMTIsemgtQ04semgtQ04semgsMCxbb2JqZWN0IE1lZGlhQ2FwYWJpbGl0aWVzXSxbb2JqZWN0IE1lZGlhU2Vzc2lvbl0sW29iamVjdCBNaW1lVHlwZUFycmF5XSx0cnVlLFtvYmplY3QgUGVybWlzc2lvbnNdLFdpbjMyLFtvYmplY3QgUGx1Z2luQXJyYXldLEdlY2tvLDIwMDMwMTA3LFtvYmplY3QgVXNlckFjdGl2YXRpb25dLE1vemlsbGEvNS4wIChXaW5kb3dzIE5UIDEwLjA7IFdpbjY0OyB4NjQpIEFwcGxlV2ViS2l0LzUzNy4zNiAoS0hUTUwsIGxpa2UgR2Vja28pIENocm9tZS85Mi4wLjQ1MTUuMTMxIFNhZmFyaS81MzcuMzYsR29vZ2xlIEluYy4sLFtvYmplY3QgRGVwcmVjYXRlZFN0b3JhZ2VRdW90YV0sW29iamVjdCBEZXByZWNhdGVkU3RvcmFnZVF1b3RhXSwxMDgwLDAsMCwxOTIwLDI0LDEwODAsW29iamVjdCBTY3JlZW5PcmllbnRhdGlvbl0sMjQsMTkyMCxbb2JqZWN0IERPTVN0cmluZ0xpc3RdLGZ1bmN0aW9uIGFzc2lnbigpIHsgW25hdGl2ZSBjb2RlXSB9LCxtYXRjaC55dWFucmVueHVlLmNvbSxtYXRjaC55dWFucmVueHVlLmNvbSxodHRwczovL21hdGNoLnl1YW5yZW54dWUuY29tL21hdGNoLzE0LGh0dHBzOi8vbWF0Y2gueXVhbnJlbnh1ZS5jb20sL21hdGNoLzE0LCxodHRwczosZnVuY3Rpb24gcmVsb2FkKCkgeyBbbmF0aXZlIGNvZGVdIH0sZnVuY3Rpb24gcmVwbGFjZSgpIHsgW25hdGl2ZSBjb2RlXSB9LCxmdW5jdGlvbiB0b1N0cmluZygpIHsgW25hdGl2ZSBjb2RlXSB9LGZ1bmN0aW9uIHZhbHVlT2YoKSB7IFtuYXRpdmUgY29kZV0gfQ==;"
    }
    r = session.get(f'https://match.yuanrenxue.com/api/match/14/m', headers=headers1, verify=False)
    r1 = requests.post(f'https://tool.yuanrenxue.com/api/ob2',  data={'m': r.text}, verify=False).json()
    v14 = re.findall(r'(window\["v14"\] = ")(.*)', r1['result'])[0][1].replace('";', "")
    v142 = re.findall(r'(window\["v142"\] = ")(.*)', r1['result'])[0][1].replace('";', "")
    sessionid = r.cookies.get('sessionid')
    # print(v14)
    # print(v142)
    # print(sessionid)
    cookies = {
        'm': jie(v14, v142, pa)[0],
        'mz': jie(v14, v142, pa)[1],
        'sessionid':'xexwqahsdzxv9ak5ec4q2xozc32r2yel'
    }
    print(cookies['sessionid'])
    session.cookies.update(cookies)
    session.cookies.set('sessionid', 'xexwqahsdzxv9ak5ec4q2xozc32r2yel')
    # session.cookies.set(jie(v14, v142, pa)[1].replace('path=/', "").split('=')[0], jie(v14, v142, pa)[1].replace('path=/', "").split('=')[1])
    headers = {
        "Accept": "*/*",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Connection": "keep-alive",
        "Host": "match.yuanrenxue.com",
        "Referer": "https://match.yuanrenxue.com/match/14",
        "User-Agent": "yuanrenxue.project",
        "X-Requested-With": "XMLHttpRequest",
        # "Cookie": f""
    }
    r2 = session.get(f'https://match.yuanrenxue.com/api/match/14?page={pa}', headers=headers, verify=False).json()
    print(r2)
    for i in r2['data']:
        num += i['value']


if __name__ == "__main__":
    num = 0
    for i in range(1, 6):
        main(i)
        # input(i)
    print(num)
